package com.lv.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.lv.db.entity.LinkMan;
import com.lv.utils.L;
import com.lv.xulin.demopro.R;

import java.util.ArrayList;
import java.util.List;


public class RvLinkManAdapter extends BaseRecyclerAdapter<RvLinkManAdapter.LinkManHolder> implements View.OnClickListener, View.OnLongClickListener {
    private Context context;
    private List<LinkMan> list;
    private int resource;
    private boolean hasChecked = false;

    public RvLinkManAdapter(Context context, List<LinkMan> list, int resource) {
        this.resource = resource;
        this.context = context;
        this.list = list;
    }

    public RvLinkManAdapter(Context context, List<LinkMan> list, int resource, boolean hasChecked) {
        this.context = context;
        this.resource = resource;
        this.list = list;
        this.hasChecked = hasChecked;
    }

    @Override
    public int getAdapterItemCount() {
        return list.size();
    }

    @Override
    public LinkManHolder getViewHolder(View view) {
        return new LinkManHolder(view);
    }

    @Override
    public LinkManHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(parent.getContext()).inflate(resource, parent, false);
        LinkManHolder holder = new LinkManHolder(v);
        holder.itemView.setOnClickListener(this);
        holder.itemView.setOnLongClickListener(this);
        holder.cbxSelected.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(LinkManHolder holder, final int position, boolean isItem) {
        LcEntityToLayout(holder, position);
    }


    public void LcEntityToLayout(final LinkManHolder holder, final int position) {

        LinkMan linkMan = list.get(position);
        holder.tvName.setText(linkMan.getFname());
        holder.tvPhone.setText(linkMan.getFphone());

        // 根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);
        // 如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section)) {
            holder.tvIndex.setVisibility(View.VISIBLE);
            holder.tvIndex.setText(convertSymbol(linkMan.getFspellsort()));
        } else {
            holder.tvIndex.setVisibility(View.GONE);
        }

        holder.cbxSelected.setChecked(getChecked().contains(linkMan));

        //判断是否显示复选框
        if (!hasChecked) {
            holder.cbxSelected.setVisibility(View.GONE);
        } else {
            holder.cbxSelected.setVisibility(View.VISIBLE);
        }

        holder.itemView.setTag(position);
        holder.cbxSelected.setTag(position);
    }

    private String convertSymbol(String s) {
        //如果不是A-Z，则替换为#号
        if (s.matches("[A-Z]")) {
            return s;
        }
        return "#";
    }


    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        LinkMan man = list.get(position);
        if (v instanceof CheckBox) {
            if (((CheckBox) v).isChecked()) {
                checkedList.add(man);
            } else {
                checkedList.remove(man);
            }
        } else {
            onItemSelected(v, position);
        }

        if (onRvItemClickLitener != null) {
            onRvItemClickLitener.onItemClick(v, position);
        }
    }

    private void onItemSelected(final View itemView, final int position) {
        LinkMan man = list.get(position);
        CheckBox cbxSelected = (CheckBox) itemView.findViewById(R.id.cbxSelected);
        if (!cbxSelected.isChecked()) {
            cbxSelected.setChecked(true);
            checkedList.add(man);
        } else {
            cbxSelected.setChecked(false);
            checkedList.remove(man);
        }
        notifyItemChanged(position);
    }

    @Override
    public boolean onLongClick(View v) {
        int position = (int) v.getTag();
        if (onRvItemClickLitener != null) {
            onRvItemClickLitener.onItemLongClick(v, position);
        }
        return true;
    }

    class LinkManHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private TextView tvPhone;
        private TextView tvIndex;
        private CheckBox cbxSelected;

        public LinkManHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tvName);
            tvPhone = (TextView) itemView.findViewById(R.id.tvPhone);
            tvIndex = (TextView) itemView.findViewById(R.id.tvIndex);
            cbxSelected = (CheckBox) itemView.findViewById(R.id.cbxSelected);
        }
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return convertSymbol(list.get(position).getFspellsort()).charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr = convertSymbol(list.get(i).getFspellsort());
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    private List<LinkMan> checkedList = new ArrayList<>();

    public List<LinkMan> getChecked() {
        return checkedList;
    }

    public void removeChecked(List<LinkMan> removeList) {
        List<LinkMan> list=new ArrayList<>();
        for (int i = 0; i < checkedList.size(); i++) {
            for (int j = 0; j < removeList.size(); j++) {
                if (checkedList.get(i).getFid().equals(removeList.get(j).getFid())) {
                    L.i(checkedList.get(i).getFname());
                    list.add(checkedList.get(i));
                }
            }
        }
        checkedList.removeAll(list);
        notifyDataSetChanged();
    }


    private OnRvItemClickLitener onRvItemClickLitener;

    public void setOnRvItemClickLitener(OnRvItemClickLitener onRvItemClickLitener) {
        this.onRvItemClickLitener = onRvItemClickLitener;
    }

    public interface OnRvItemClickLitener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v, int position);
    }
}
